#include"C++_thread_cond.hpp"

pthread_mutex_t mutex;

void* start_push(void *args)
{
    cond_task<task, std::string>* test_cond = (cond_task<task, std::string>*)args;
    char op[5] = {'+', '-', '*', '/', '%'};
    while(true)
    {
        //任务分配区
        int x = rand()%100;
        int y = rand()%30;
        task _test(x, y, op[rand()%5]);

        //临界资源区
        pthread_mutex_lock(&mutex);       
        test_cond->push(_test, &mutex);
        std::cout << "线程："<<pthread_self() << "载入数据"<< _test.task_name() <<std::endl;
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}

void* start_get(void *args)
{
    cond_task<task, std::string>* test_cond = (cond_task<task, std::string>*)args;
    while(true)
    {
        char buff[64];
        pthread_mutex_lock(&mutex);        
        strcpy(buff, (test_cond->get(&mutex)()).c_str());
        test_cond->store_to_file(buff);
        std::cout << "线程："<<pthread_self() << "拿出数据" << buff <<std::endl;
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}



int main()
{
    std::vector<pthread_t> threads(4);
    pthread_mutex_init(&mutex, NULL);
    cond_task<task, std::string> test_cond;
    
    
    pthread_create(&threads[0], NULL, start_get, (void*)&test_cond);
    pthread_create(&threads[1], NULL, start_get, (void*)&test_cond);
    pthread_create(&threads[2], NULL, start_push, (void*)&test_cond);
    pthread_create(&threads[3], NULL, start_push, (void*)&test_cond);

    for (int i = 0; i < 4; i++)
    {
        pthread_join(threads[i], NULL);
    }
    
    return 0;
}





// #include"C++_thread_cond.hpp"

// pthread_mutex_t mutex;

// void* start_get(void *args)
// {
//     thread_cond<task> * test_cond = (thread_cond<task>*)args;
//     while(true)
//     {
//         pthread_mutex_lock(&mutex);
        
//         std::cout << "线程："<<pthread_self() << "拿出数据" << test_cond->thread_cond_get(&mutex)() \
//         <<std::endl;
//         pthread_mutex_unlock(&mutex);
//         sleep(1);
//     }
// }

// void* start_push(void *args)
// {
//     thread_cond<task> * test_cond = (thread_cond<task>*)args;
//     char op[5] = {'+', '-', '*', '/', '%'};
//     while(true)
//     {
//         //任务分配区
//         int x = rand()%100;
//         int y = rand()%30;
//         task _test(x, y, op[rand()%5]);

//         //临界资源区
//         pthread_mutex_lock(&mutex);       
//         test_cond->thread_cond_push(_test, &mutex);
//         std::cout << "线程："<<pthread_self() << "载入数据"<< _test.task_name() <<std::endl;
//         pthread_mutex_unlock(&mutex);
//         sleep(1);
//     }
// }

// int main()
// {
//     thread_cond<task> test_cond;
//     pthread_mutex_init(&mutex, NULL);
//     std::vector<pthread_t> threads(4);
//     pthread_create(&threads[0], NULL, start_get, (void*)&test_cond);
//     pthread_create(&threads[1], NULL, start_get, (void*)&test_cond);
//     pthread_create(&threads[2], NULL, start_push, (void*)&test_cond);
//     pthread_create(&threads[3], NULL, start_push, (void*)&test_cond);

//     for (int i = 0; i < 4; i++)
//     {
//         pthread_join(threads[i], NULL);
//     }
    
//     return 0;
// }